import java.util.HashSet;
import java.util.Set;

/**
 * 217. 存在重复元素
 * https://leetcode.cn/problems/contains-duplicate/description/
 */
class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet();
        for(int i = 0; i < nums.length; i++) {
//            for(int x : nums) {  或者写成这样子
            if(set.contains(nums[i])) return true;
            set.add(nums[i]);
        }
        return false;
    }

    /**
     * 面试题 01.02. 判定是否互为字符重排
     * https://leetcode.cn/problems/check-permutation-lcci/description/
     */
    public boolean CheckPermutation(String s1, String s2) {
        if(s1.length() != s2.length()) return false;
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        int[] hash = new int[26];

        for(int i = 0; i < s1.length(); i++) {
            hash[c1[i] - 'a']++;
        }
        for(int i = 0; i < s2.length(); i++) {
            hash[c2[i] - 'a']--;
            if(hash[c2[i] - 'a'] < 0) return false;
        }
        // for(int i = 0; i < 26; i++) {
        //     if(hash[i] > 0) return false;
        // }

        return true;
    }
}